package hhm.android.myframe.room

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.room.Room
import hhm.android.base.activity.BaseActivity
import hhm.android.myframe.R

class DataBaseActivity : BaseActivity() {

    lateinit var dataBase: BabyDataBase
    lateinit var babyDao: DataBaseDao

    lateinit var buttonInsert: Button
    lateinit var buttonDelete: Button
    lateinit var buttonUpdate: Button
    lateinit var buttonClear: Button
    lateinit var text: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_database)

        dataBase = Room.databaseBuilder(this, BabyDataBase::class.java, "baby_database")
            .allowMainThreadQueries().build()
        babyDao = dataBase.getBabyDao()

        buttonClear = findViewById(R.id.clear)
        buttonInsert = findViewById(R.id.add)
        buttonUpdate = findViewById(R.id.update)
        buttonDelete = findViewById(R.id.delete)
        text = findViewById(R.id.stextview)

        refreshView()

        buttonClear.setOnClickListener {

        }
        buttonInsert.setOnClickListener {
            val b1 = BabyModel("阿拉多", 7)
            val b2 = BabyModel("阿拉噶", 9)
            babyDao.addBaby(listOf(b1, b2))
            refreshView()
        }
        buttonUpdate.setOnClickListener {

        }
        buttonDelete.setOnClickListener {

        }
    }

    private fun refreshView() {
        val list = babyDao.queryBaby()
        val sb = StringBuilder()
        list.forEach {
            sb.append("id:").append(it.id).append("  name:").append(it.name)
                .append("  age:").append(it.age).append("\n")
        }
        text.text = sb.toString()
    }
}